package cn.com.cfae.iras.doc.analyze.handUtil;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.parse.kv.TextLayout;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.PageModel;
import cn.com.cfae.iras.doc.analyze.parse.model.SectionModel;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class SearchKeywordUtil extends BaseComponent {

    private static final Logger logger = LoggerFactory.getLogger(SearchKeywordUtil.class);

    //获取二级标题，同义词以‘，’分隔
    public Map<String,Object> getSectionTitlePageFromStartPage(List<SectionModel> sectionModelList, String synonyms, int startPageNum,int endPageNum){
        List<ComponentResult.TextPosition> textPositions = new ArrayList<>();
        ComponentResult.TextPosition contentAndPosition =null;
        Map<String, Object> result = new HashMap<>();
        if (StringUtils.isEmpty(synonyms)|| CollectionUtils.isEmpty(sectionModelList)){
            result.put("flag",false);
            result.put("startPage",null);
            result.put("endPage",null);
            result.put("textPositions",textPositions);
            return result;
        }
        List<String> keywordList = new ArrayList<>();

        if (synonyms.contains(",")){
            String[]  keywords = synonyms.split(",");
            for (String keyword:keywords){
                keywordList.add(keyword);
            }
        }else {
            keywordList.add(synonyms);
        }

        int startPage  = startPageNum;
        int endPage = endPageNum;
        String title = null;
        Pattern pattern = null;
        Matcher matcher = null;
        boolean isExist = false;

        for (SectionModel sectionModel : sectionModelList){
            if (sectionModel.getStartPage()<startPageNum){
                continue;
            }
            if (endPageNum!=0&&sectionModel.getEndPage()>endPageNum){
                break;
            }
            title = sectionModel.getTitle();
            if (StringUtils.isNotEmpty(title)) {
                for (String keyword : keywordList) {
                    pattern = Pattern.compile(keyword);

                    matcher = pattern.matcher(title);
                    if (matcher.find()) {
                        contentAndPosition = new ComponentResult().new TextPosition();
                        TextLayout textLayout = sectionModel.getTextLayout();
                        if (textLayout!=null){
                            contentAndPosition.setX(textLayout.getLeft());
                            contentAndPosition.setY(textLayout.getTop());
                            contentAndPosition.setContent(textLayout.getText());
                            textPositions.add(contentAndPosition);
                        }
                        startPage = sectionModel.getStartPage();
                        int i = sectionModelList.indexOf(sectionModel);
                        SectionModel childSectionModel = null;
                        if (i+1<sectionModelList.size()){
                            childSectionModel = sectionModelList.get(i + 1);
                            endPage = childSectionModel.getStartPage();
                        }else {
                            childSectionModel = sectionModelList.get(i);
                            endPage = childSectionModel.getStartPage();
                        }
                        isExist = true;
                        break;
                    }

                }
            }
            if (isExist)
                break;
        }
        if (isExist){
            result.put("flag",isExist);
            result.put("startPage",startPage);
            result.put("endPage",endPage);
            result.put("textPositions",textPositions);
        }else {
            result = getThirdTitlePageFromStartPage(sectionModelList, synonyms, startPageNum, endPageNum);

        }

        return result;
    }


    //获取三级标题，同义词以‘，’分隔
    public Map<String,Object> getThirdTitlePageFromStartPage(List<SectionModel> sectionModelList, String synonyms, int startPageNum,int endPageNum){
        List<ComponentResult.TextPosition> textPositions = new ArrayList<>();
        ComponentResult.TextPosition contentAndPosition =null;
        Map<String, Object> result = new HashMap<>();
        if (StringUtils.isEmpty(synonyms)|| CollectionUtils.isEmpty(sectionModelList)){
            result.put("flag",false);
            result.put("startPage",null);
            result.put("endPage",null);
            result.put("textPositions",textPositions);
            return result;
        }
        List<String> keywordList = new ArrayList<>();

        if (synonyms.contains(",")){
            String[]  keywords = synonyms.split(",");
            for (String keyword:keywords){
                keywordList.add(keyword);
            }
        }else {
            keywordList.add(synonyms);
        }

        int startPage  = startPageNum;
        int endPage = endPageNum;
        String title = null;
        Pattern pattern = null;
        Matcher matcher = null;
        boolean isExist = false;
        for (SectionModel sectionModel : sectionModelList){
            if (sectionModel.getStartPage()<startPageNum){
                continue;
            }
            if (endPageNum!=0&&sectionModel.getEndPage()>endPageNum){
                break;
            }
            List<SectionModel> childSectionModelList = sectionModel.getChildSectionModelList();
            if (CollectionUtils.isNotEmpty(childSectionModelList)){
                for (SectionModel childSectionModel:childSectionModelList){
                    title = childSectionModel.getTitle();
                    if (StringUtils.isNotEmpty(title)) {
                        for (String keyword : keywordList) {
                            pattern = Pattern.compile(keyword);
                            matcher = pattern.matcher(title);
                            if (matcher.find()) {
                                contentAndPosition = new ComponentResult().new TextPosition();
                                TextLayout textLayout = childSectionModel.getTextLayout();
                                if (textLayout!=null){
                                    contentAndPosition.setX(textLayout.getLeft());
                                    contentAndPosition.setY(textLayout.getTop());
                                    contentAndPosition.setContent(textLayout.getText());
                                    textPositions.add(contentAndPosition);
                                }
                                startPage = childSectionModel.getStartPage();
                                int i = sectionModelList.indexOf(sectionModel);
                                SectionModel child = null;
                                if (i+1<sectionModelList.size()){
                                    child = sectionModelList.get(i + 1);
                                    endPage = child.getStartPage();
                                }else {
                                    child = sectionModelList.get(i);
                                    endPage = child.getStartPage();
                                }
                                isExist = true;
                                break;
                            }
                        }
                    }
                    if (isExist)
                        break;
                }
            }

            if (isExist)
                break;

        }
        result.put("flag",isExist);
        result.put("startPage",startPage);
        result.put("endPage",endPage);
        result.put("textPositions",textPositions);

        return result;
    }



   /* public Map<String,Object> isContainsKeywords2LiensOfSynonyms(JsonElement jsonElement, String synonyms){
        Map<String, Object> reslt = new HashMap<>();
        List<String> keywordList = new ArrayList<>();
        if (StringUtils.isNotEmpty(synonyms)){
            if (synonyms.contains(",")){
                String[]  keywords = synonyms.split(",");
                for (String keyword:keywords){
                    keywordList.add(keyword);
                }
            }else {
                keywordList.add(synonyms);
            }
        }

        List<ComponentResult.TextPosition> textPositions = new ArrayList<>();
        ComponentResult.TextPosition contentAndPosition =null;

        JsonArray jsonArray = null;
        if (jsonElement.isJsonArray()){
            jsonArray = jsonElement.getAsJsonArray();
        }
        boolean flag = false;
        Double objX = 0.0;
        Double objY = 0.0;
        Pattern pattern = null;
        Matcher matcher = null;
        if (!jsonArray.isJsonNull()){
            for (int i=0;i<jsonArray.size();i++){

                JsonObject asJsonObject = jsonArray.get(i).getAsJsonObject();
                Set set = asJsonObject.entrySet();
                Iterator<Map.Entry<String, JsonElement>> it = set.iterator();
                String key ;

                while (it.hasNext()){
                    Map.Entry<String, JsonElement> entry = it.next();
                    key= entry.getKey();
                    if (StringUtils.isNotEmpty(key)&&key.equals("top")){
                        objY =  Double.parseDouble(entry.getValue().getAsString());
                    }
                    if (StringUtils.isNotEmpty(key)&&key.equals("left")){
                        objX = Double.parseDouble(entry.getValue().getAsString());
                    }
                    if (StringUtils.isNotEmpty(key)&&key.equals("data")){//当前章内容
                        JsonArray dataJsonArray = null;
                        if (entry.getValue().isJsonArray()){
                            dataJsonArray  = entry.getValue().getAsJsonArray();
                        }
                        if (!dataJsonArray.isJsonNull()){
                            for (int d=0; d<dataJsonArray.size(); d++){//当前章循环行
                                JsonArray delmJsonArray = dataJsonArray.get(d).getAsJsonArray();
                                if (!dataJsonArray.isJsonNull()){
                                    for (int de=0;de<delmJsonArray.size();de++){
                                        JsonObject deJsonObject = delmJsonArray.get(de).getAsJsonObject();//当前行
                                        Set deSet = deJsonObject.entrySet();
                                        Iterator<Map.Entry<String, JsonElement>> deIt = deSet.iterator();
                                        Double x = 0.0;
                                        Double y = 0.0;
                                        while (deIt.hasNext()){
                                            Map.Entry<String, JsonElement> deEntry = deIt.next();
                                            String deKey= deEntry.getKey();
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("top")){
                                                y = Double.parseDouble(deEntry.getValue().getAsString());
                                            }
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("left")){
                                                x =  Double.parseDouble(deEntry.getValue().getAsString());
                                            }
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("text")){
                                                boolean isExist = false;
                                                String count = deEntry.getValue().getAsString();

                                                if (StringUtils.isNotEmpty(count)){

                                                    ComponentResult.TextPosition frontTextPostion = getFrontString(dataJsonArray, d);


                                                    //当前行

                                                    //后一行
                                                    ComponentResult.TextPosition behindTextPostion = getBehindString(dataJsonArray, d);

                                                    if (CollectionUtils.isNotEmpty(keywordList)){
                                                        for (String keyword :keywordList){
                                                            //判断是否在当前行

                                                            if (!isExist&&count.contains(keyword)){
                                                                contentAndPosition = new ComponentResult().new TextPosition();
                                                                flag = true;
                                                                isExist = true;
                                                                int countBegenIndex = count.indexOf(keyword)+1;
                                                                int countEndIndex = countBegenIndex+keyword.length()-1;
                                                                contentAndPosition.setX(x);
                                                                contentAndPosition.setY(y);
                                                                contentAndPosition.setContent(count.substring(countBegenIndex-1,countEndIndex));
                                                                textPositions.add(contentAndPosition);
                                                                reslt.put("flag",flag);
                                                                reslt.put("position",textPositions);
                                                                break;
                                                            }

                                                            //判断是否在后一行
                                                            if (!isExist&&StringUtils.isNotEmpty(behindTextPostion.getContent())){
                                                                String behindText = behindTextPostion.getContent();
                                                                if (behindText.contains(keyword)){
                                                                    contentAndPosition = new ComponentResult().new TextPosition();
                                                                    flag = true;
                                                                    isExist = true;
                                                                    int countBegenIndex = behindText.indexOf(keyword)+1;
                                                                    int countEndIndex = countBegenIndex+keyword.length()-1;
                                                                    contentAndPosition.setX(behindTextPostion.getX());
                                                                    contentAndPosition.setY(behindTextPostion.getY());
                                                                    contentAndPosition.setContent(behindText.substring(countBegenIndex-1,countEndIndex));
                                                                    textPositions.add(contentAndPosition);
                                                                    reslt.put("flag",flag);
                                                                    reslt.put("position",textPositions);
                                                                    break;
                                                                }

                                                            }
                                                            //判断前一行 和当前行
                                                            if (!isExist&&StringUtils.isNotEmpty(frontTextPostion.getContent())){

                                                                //前一行文本
                                                                String fornText = frontTextPostion.getContent();
                                                                //总文本
                                                                String temp = fornText+count;
                                                                if (temp.contains(keyword)){
                                                                    flag = true;
                                                                    isExist = true;
                                                                    //当前关键词的位置
                                                                    int countKeywrodIndex = temp.indexOf(keyword)+1;
                                                                    //关键词长度
                                                                    int keywrodLenth = keyword.length();
                                                                    //关键词结尾位置
                                                                    int keywordIndexEndLenth = keywrodLenth+countKeywrodIndex-1;//删除掉开头
                                                                    //前一行长度
                                                                    int frontLenth = fornText.length();
                                                                    //第二行结束位置
                                                                    int endIndex = keywordIndexEndLenth-countKeywrodIndex-1;

                                                                    if (frontLenth>=countKeywrodIndex){
                                                                        contentAndPosition = new ComponentResult().new TextPosition();
                                                                        contentAndPosition.setX(frontTextPostion.getX());
                                                                        contentAndPosition.setY(frontTextPostion.getY());
                                                                        contentAndPosition.setContent(fornText.substring(countKeywrodIndex-1,frontLenth));
                                                                        textPositions.add(contentAndPosition);
                                                                    }
                                                                    if (endIndex<=count.length()){
                                                                        contentAndPosition = new ComponentResult().new TextPosition();
                                                                        contentAndPosition.setX(x);
                                                                        contentAndPosition.setY(y);
                                                                        contentAndPosition.setContent(count.substring(0,endIndex));
                                                                        textPositions.add(contentAndPosition);
                                                                    }
                                                                    reslt.put("flag",flag);
                                                                    reslt.put("position",textPositions);
                                                                    break;
                                                                }
                                                            }
                                                            //判断当前行和后一行
                                                            if (!isExist&&StringUtils.isNotEmpty(behindTextPostion.getContent())){
                                                                //后一行的文本
                                                                String behindText = behindTextPostion.getContent();
                                                                //总文本
                                                                String temp = count+behindText;

                                                                if (temp.contains(keyword)){
                                                                    flag = true;
                                                                    isExist = true;

                                                                    //当前关键词的位置
                                                                    int countKeywrodIndex = temp.indexOf(keyword)+1;
                                                                    //关键词长度
                                                                    int keywrodLenth = keyword.length();
                                                                    //关键词结尾位置
                                                                    int keywordIndexEndLenth = keywrodLenth+countKeywrodIndex-1;//删除掉开头
                                                                    //当前行长度
                                                                    int coumtLenth = count.length();
                                                                    //第二行结束位置
                                                                    int endIndex = keywordIndexEndLenth-coumtLenth;

                                                                    if (coumtLenth>=countKeywrodIndex){
                                                                        contentAndPosition = new ComponentResult().new TextPosition();
                                                                        contentAndPosition.setX(x);
                                                                        contentAndPosition.setY(y);
                                                                        contentAndPosition.setContent(count.substring(countKeywrodIndex-1,coumtLenth));
                                                                        textPositions.add(contentAndPosition);
                                                                    }
                                                                    if (endIndex<=behindText.length()){
                                                                        contentAndPosition = new ComponentResult().new TextPosition();
                                                                        contentAndPosition.setX(behindTextPostion.getX());
                                                                        contentAndPosition.setY(behindTextPostion.getY());
                                                                        contentAndPosition.setContent(behindText.substring(0,endIndex));
                                                                        textPositions.add(contentAndPosition);
                                                                    }

                                                                    reslt.put("flag",flag);
                                                                    reslt.put("position",textPositions);
                                                                    break;

                                                                }

                                                            }
                                                        }
                                                    }


                                                }

                                            }
                                        }
                                        if (flag){
                                            break;
                                        }
                                    }
                                }
                                if (flag){
                                    break;
                                }
                            }
                        }
                    }
                    if (flag){
                        break;
                    }

                }
                if (!flag){
                    contentAndPosition = new ComponentResult().new TextPosition();
                    contentAndPosition.setX(objX);
                    contentAndPosition.setY(objY);
                    contentAndPosition.setContent("");
                    textPositions.add(contentAndPosition);
                    reslt.put("flag",flag);
                    reslt.put("position",textPositions);
                }
                if (flag)
                    break;
            }


        }

        return reslt;
    }*/

    public Map<String,Object> isContainsKeywords2LiensOfSynonyms(JsonElement jsonElement, String synonyms,PageModel pageModel){
        Map<String, Object> reslt = new HashMap<>();
        List<String> keywordList = new ArrayList<>();
        if (StringUtils.isNotEmpty(synonyms)){
            if (synonyms.contains(",")){
                String[]  keywords = synonyms.split(",");
                for (String keyword:keywords){
                    keywordList.add(keyword);
                }
            }else {
                keywordList.add(synonyms);
            }
        }

        List<ComponentResult.TextPosition> textPositions = new ArrayList<>();
        ComponentResult.TextPosition contentAndPosition =null;

        JsonArray jsonArray = null;
        if (jsonElement.isJsonArray()){
            jsonArray = jsonElement.getAsJsonArray();
        }
        boolean flag = false;
        Double objX = 0.0;
        Double objY = 0.0;
        Pattern pattern = null;
        Matcher matcher = null;
        if (!jsonArray.isJsonNull()){
            for (int i=0;i<jsonArray.size();i++){

                JsonObject asJsonObject = jsonArray.get(i).getAsJsonObject();
                Set set = asJsonObject.entrySet();
                Iterator<Map.Entry<String, JsonElement>> it = set.iterator();
                String key ;

                while (it.hasNext()){
                    Map.Entry<String, JsonElement> entry = it.next();
                    key= entry.getKey();
                    if (StringUtils.isNotEmpty(key)&&key.equals("top")){
                        objY =  Double.parseDouble(entry.getValue().getAsString());
                    }
                    if (StringUtils.isNotEmpty(key)&&key.equals("left")){
                        objX = Double.parseDouble(entry.getValue().getAsString());
                    }
                    if (StringUtils.isNotEmpty(key)&&key.equals("data")){//当前章内容
                        JsonArray dataJsonArray = null;
                        if (entry.getValue().isJsonArray()){
                            dataJsonArray  = entry.getValue().getAsJsonArray();
                        }
                        if (!dataJsonArray.isJsonNull()){
                            for (int d=0; d<dataJsonArray.size(); d++){//当前章循环行
                                JsonArray delmJsonArray = dataJsonArray.get(d).getAsJsonArray();
                                if (!dataJsonArray.isJsonNull()){
                                    for (int de=0;de<delmJsonArray.size();de++){
                                        JsonObject deJsonObject = delmJsonArray.get(de).getAsJsonObject();//当前行
                                        if (pageModel!=null){
                                            if (isInTable(deJsonObject,pageModel)){
                                                continue;
                                            }
                                        }
                                        Set deSet = deJsonObject.entrySet();
                                        Iterator<Map.Entry<String, JsonElement>> deIt = deSet.iterator();
                                        Double x = 0.0;
                                        Double y = 0.0;
                                        while (deIt.hasNext()){
                                            Map.Entry<String, JsonElement> deEntry = deIt.next();
                                            String deKey= deEntry.getKey();
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("top")){
                                                y = Double.parseDouble(deEntry.getValue().getAsString());
                                            }
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("left")){
                                                x =  Double.parseDouble(deEntry.getValue().getAsString());
                                            }
                                            if (StringUtils.isNotEmpty(deKey)&&deKey.equals("text")){
                                                boolean isExist = false;
                                                String count = deEntry.getValue().getAsString();

                                                if (StringUtils.isNotEmpty(count)){

                                                 //   ComponentResult.TextPosition frontTextPostion = getFrontString(dataJsonArray, d);


                                                    //当前行

                                                    //后一行
                                                    ComponentResult.TextPosition behindTextPostion = getBehindString(dataJsonArray, d);

                                                    if (CollectionUtils.isNotEmpty(keywordList)){
                                                        for (String keyword :keywordList){
                                                            //判断是否在当前行

                                                            if (!isExist&&count.contains(keyword)){
                                                                contentAndPosition = new ComponentResult().new TextPosition();
                                                                flag = true;
                                                                isExist = true;
                                                                int countBegenIndex = count.indexOf(keyword)+1;
                                                                int countEndIndex = countBegenIndex+keyword.length()-1;
                                                                contentAndPosition.setX(x);
                                                                contentAndPosition.setY(y);
                                                                contentAndPosition.setContent(count.substring(countBegenIndex-1,countEndIndex));
                                                                textPositions.add(contentAndPosition);
                                                                reslt.put("flag",flag);
                                                                reslt.put("position",textPositions);
                                                                break;
                                                            }

                                                            //判断是否在后一行
                                                            if (!isExist&&StringUtils.isNotEmpty(behindTextPostion.getContent())){
                                                                String behindText = behindTextPostion.getContent();
                                                                if (behindText.contains(keyword)){
                                                                    contentAndPosition = new ComponentResult().new TextPosition();
                                                                    flag = true;
                                                                    isExist = true;
                                                                    int countBegenIndex = behindText.indexOf(keyword)+1;
                                                                    int countEndIndex = countBegenIndex+keyword.length()-1;
                                                                    contentAndPosition.setX(behindTextPostion.getX());
                                                                    contentAndPosition.setY(behindTextPostion.getY());
                                                                    contentAndPosition.setContent(behindText.substring(countBegenIndex-1,countEndIndex));
                                                                    textPositions.add(contentAndPosition);
                                                                    reslt.put("flag",flag);
                                                                    reslt.put("position",textPositions);
                                                                    break;
                                                                }

                                                            }
                                                            //判断前一行 和当前行
                                                           /* if (!isExist&&StringUtils.isNotEmpty(frontTextPostion.getContent())){

                                                                //前一行文本
                                                                String fornText = frontTextPostion.getContent();
                                                                //总文本
                                                                String temp = fornText+count;
                                                                if (temp.contains(keyword)){
                                                                    flag = true;
                                                                    isExist = true;
                                                                    //当前关键词的位置
                                                                    int countKeywrodIndex = temp.indexOf(keyword)+1;
                                                                    //关键词长度
                                                                    int keywrodLenth = keyword.length();
                                                                    //关键词结尾位置
                                                                    int keywordIndexEndLenth = keywrodLenth+countKeywrodIndex-1;//删除掉开头
                                                                    //前一行长度
                                                                    int frontLenth = fornText.length();
                                                                    //第二行结束位置
                                                                    //int endIndex = keywordIndexEndLenth-countKeywrodIndex-1;
                                                                    int endIndex = keywordIndexEndLenth-frontLenth-1;

                                                                    if (frontLenth>=keywordIndexEndLenth){
                                                                        contentAndPosition = new ComponentResult().new TextPosition();
                                                                        contentAndPosition.setX(frontTextPostion.getX());
                                                                        contentAndPosition.setY(frontTextPostion.getY());
                                                                        contentAndPosition.setContent(fornText.substring(countKeywrodIndex-1,frontLenth));
                                                                        textPositions.add(contentAndPosition);
                                                                    }else {
                                                                        if (frontLenth>=countKeywrodIndex){
                                                                            contentAndPosition = new ComponentResult().new TextPosition();
                                                                            contentAndPosition.setX(frontTextPostion.getX());
                                                                            contentAndPosition.setY(frontTextPostion.getY());
                                                                            contentAndPosition.setContent(fornText.substring(countKeywrodIndex-1,frontLenth));
                                                                            textPositions.add(contentAndPosition);
                                                                        }
                                                                        if (endIndex<=count.length()){
                                                                            contentAndPosition = new ComponentResult().new TextPosition();
                                                                            contentAndPosition.setX(x);
                                                                            contentAndPosition.setY(y);
                                                                            contentAndPosition.setContent(count.substring(0,endIndex));
                                                                            textPositions.add(contentAndPosition);
                                                                        }
                                                                    }

                                                                    reslt.put("flag",flag);
                                                                    reslt.put("position",textPositions);
                                                                    break;
                                                                }
                                                            }*/
                                                            //判断当前行和后一行
                                                            if (!isExist&&StringUtils.isNotEmpty(behindTextPostion.getContent())){
                                                                //后一行的文本
                                                                String behindText = behindTextPostion.getContent();
                                                                //总文本
                                                                String temp = count+behindText;

                                                                if (temp.contains(keyword)){
                                                                    flag = true;
                                                                    isExist = true;

                                                                    //当前关键词的位置
                                                                    int countKeywrodIndex = temp.indexOf(keyword)+1;
                                                                    //关键词长度
                                                                    int keywrodLenth = keyword.length();
                                                                    //关键词结尾位置
                                                                    int keywordIndexEndLenth = keywrodLenth+countKeywrodIndex-1;//删除掉开头
                                                                    //当前行长度
                                                                    int coumtLenth = count.length();
                                                                    //第二行结束位置
                                                                    int endIndex = keywordIndexEndLenth-coumtLenth;

                                                                    if (coumtLenth>=countKeywrodIndex){
                                                                        contentAndPosition = new ComponentResult().new TextPosition();
                                                                        contentAndPosition.setX(x);
                                                                        contentAndPosition.setY(y);
                                                                        contentAndPosition.setContent(count.substring(countKeywrodIndex-1,coumtLenth));
                                                                        textPositions.add(contentAndPosition);
                                                                    }
                                                                    if (endIndex<=behindText.length()){
                                                                        contentAndPosition = new ComponentResult().new TextPosition();
                                                                        contentAndPosition.setX(behindTextPostion.getX());
                                                                        contentAndPosition.setY(behindTextPostion.getY());
                                                                        contentAndPosition.setContent(behindText.substring(0,endIndex));
                                                                        textPositions.add(contentAndPosition);
                                                                    }

                                                                    reslt.put("flag",flag);
                                                                    reslt.put("position",textPositions);
                                                                    break;

                                                                }

                                                            }
                                                        }
                                                    }


                                                }

                                            }
                                        }
                                        if (flag){
                                            break;
                                        }
                                    }
                                }
                                if (flag){
                                    break;
                                }
                            }
                        }
                    }
                    if (flag){
                        break;
                    }

                }
                if (!flag){
                    contentAndPosition = new ComponentResult().new TextPosition();
                    contentAndPosition.setX(objX);
                    contentAndPosition.setY(objY);
                    contentAndPosition.setContent("");
                    textPositions.add(contentAndPosition);
                    reslt.put("flag",flag);
                    reslt.put("position",textPositions);
                }
                if (flag)
                    break;
            }


        }

        return reslt;
    }

    //前一行
    private  ComponentResult.TextPosition  getFrontString(JsonArray dataJsonArray, int row){
        ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();
        Double x = 0.0;
        Double y = 0.0;
        boolean flag = false;
        if (dataJsonArray.isJsonNull())
            return textPosition;

        JsonArray delmJsonArray = null;
        if (row!=0){
            delmJsonArray = dataJsonArray.get(row-1).getAsJsonArray();
            if (!delmJsonArray.isJsonNull()){
                for (int de=0;de<delmJsonArray.size();de++){
                    JsonObject deJsonObject = delmJsonArray.get(de).getAsJsonObject();//当前行
                    Set deSet = deJsonObject.entrySet();
                    Iterator<Map.Entry<String, JsonElement>> deIt = deSet.iterator();
                    while (deIt.hasNext()){
                        Map.Entry<String, JsonElement> deEntry = deIt.next();
                        String deKey= deEntry.getKey();
                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("top")){
                            y = Double.parseDouble(deEntry.getValue().getAsString());
                        }
                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("left")){
                            x =  Double.parseDouble(deEntry.getValue().getAsString());
                        }
                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("text")){
                            String content = deEntry.getValue().toString();
                            if (StringUtils.isNotEmpty(content))
                                content = content.replaceAll("\"","");
                            textPosition.setX(x);
                            textPosition.setY(y);
                            textPosition.setContent(content);
                            flag = true;
                            break;
                        }
                    }
                    if (flag){
                        break;
                    }
                }
            }
        }else {
            return textPosition;
        }

        return textPosition;
    }

    //后一行
    private ComponentResult.TextPosition getBehindString(JsonArray dataJsonArray,int row){
        ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();
        Double x = 0.0;
        Double y = 0.0;
        boolean flag = false;
        if (dataJsonArray.isJsonNull())
            return textPosition;

        JsonArray delmJsonArray = null;
        if (row<=dataJsonArray.size()-2) {
            delmJsonArray = dataJsonArray.get(row + 1).getAsJsonArray();
            if (!delmJsonArray.isJsonNull()){
                for (int de=0;de<delmJsonArray.size();de++){
                    JsonObject deJsonObject = delmJsonArray.get(de).getAsJsonObject();//当前行
                    Set deSet = deJsonObject.entrySet();
                    Iterator<Map.Entry<String, JsonElement>> deIt = deSet.iterator();
                    while (deIt.hasNext()){
                        Map.Entry<String, JsonElement> deEntry = deIt.next();
                        String deKey= deEntry.getKey();

                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("top")){
                            y = Double.parseDouble(deEntry.getValue().getAsString());
                        }
                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("left")){
                            x =  Double.parseDouble(deEntry.getValue().getAsString());
                        }
                        if (StringUtils.isNotEmpty(deKey)&&deKey.equals("text")){
                            String content = deEntry.getValue().toString();
                            if (StringUtils.isNotEmpty(content))
                                content = content.replaceAll("\"","");
                            textPosition.setX(x);
                            textPosition.setY(y);
                            textPosition.setContent(content);
                            flag = true;
                            break;
                        }
                    }
                    if (flag){
                        break;
                    }
                }
            }
        }else {
            return textPosition;
        }

        return textPosition;
    }



    @Override
    public List<ComponentResult> handlerM() {
        return null;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        return null;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        return null;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        return null;
    }



}
